﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace _2D
{
    public class Background
    {
        private Vector2 position;
        private Texture2D backTexture;
        private int _shift;
        private Vector2 backgroundSize, screenSize;
        private int _initialYposition;
        private Vector2 origen;
        private Boolean _finalBg;

        public Background(Vector2 pos, ContentManager content, string textureName, int shift, int initialYposition, Boolean finalBg)
        {
            position = pos;
            backTexture = content.Load<Texture2D>(textureName);
            _shift = shift;
            screenSize = new Vector2(800, 600);
            backgroundSize = new Vector2(backTexture.Width, backTexture.Height);
            _initialYposition = initialYposition;
            origen = new Vector2(0, _initialYposition);
            _finalBg = finalBg;
        }

        public void Draw(SpriteBatch batch)
        {
            Rectangle sourcerect = new Rectangle(0, 0,
                backTexture.Width, backTexture.Height);

            batch.Draw(backTexture, position, sourcerect, Color.White,
                        0, origen, 1, SpriteEffects.None, 0);
        }

        public void Update(KeyboardState keyboard, bool moveLevelComponents)
        {
            if (moveLevelComponents)
            {
                if (keyboard.IsKeyDown(Keys.Up))
                {
                    position.Y += _shift;
                }
            }

            // Comprobamos el fondo
            if (Math.Abs(position.Y) + screenSize.Y >= backgroundSize.Y)
            {
                position.Y = -backgroundSize.Y + screenSize.Y;
            }
            //position.Y = -backgroundSize.Y + screenSize.Y;


            //if (_finalBg)
            //{

            //}
        }
    }
}
